package com.lyf;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

/**
 * @AUTHOR LYF
 * @DATE 2022/3/5
 * @VERSION 1.0
 * @DESC
 * 本地ES位置：D:\Development\ElasticSearchs\elasticsearch-7.12.0\bin
 *
 */
public class EsClient {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
               // new RestClientBuilder(new HttpHost("localhost",9200))
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );//Node - Host
        //Node node = new Node();
//        RequestOptions requestOptions = new RequestOptions(
//
//        );
//        restHighLevelClient.ping();

        // 1.进行索引操作
        // （1）创建索引请求对象

//        CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
//        // (2)返回对象
//        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(
//                createIndexRequest,RequestOptions.DEFAULT
//        );
//        System.out.println("索引操作返回结果:"+createIndexResponse.isAcknowledged());
        // 2.进行索引的查询
        GetIndexRequest getIndexRequest = new GetIndexRequest("user");
        GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest,RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getAliases()+":"
                +getIndexResponse.getMappings()+":"+getIndexResponse.getSettings());
        // 3.进行索引的删除
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");
        AcknowledgedResponse acknowledgedResponse= restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        System.out.println("删除成功？"+acknowledgedResponse.isAcknowledged());



        // 关闭对象
        restHighLevelClient.close();
    }
}
